package _interview100;

import java.util.HashSet;
import java.util.Set;

/**
 * 128. 最长连续序列
 */
public class No128 {
    public int longestConsecutive(int[] nums) {
        Set<Integer> hashSet = new HashSet<>();
        for (int num : nums) {
            hashSet.add(num);
        }
        int sum = 0;
        for (Integer i : hashSet) {
            if (!hashSet.contains(i - 1)) {
                int curSum = 1;
                int index = i;
                while (hashSet.contains(++index)) curSum++;
                sum = Math.max(sum, curSum);
            }
        }
        return sum;
    }
}
